package com.lg.erp.config;



import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lg.erp.entity.SMenu;
import com.lg.erp.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import java.util.List;

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private IMenuService functionsService;

    @Autowired
    private PersistentTokenRepository persistentTokenRepository;


    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
                auth.userDetailsService(userDetailsService);
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry =
                http.authorizeRequests();
        registry.antMatchers("/login","/error","/js/**","/upload","/user","/company").permitAll();
        //给每个权限的url进行权限验证
        List<SMenu> functions = functionsService.list();
        functions.forEach(functions1 -> {
                    if(StringUtils.isNotBlank(functions1.getMenuUrl()))
                        registry.antMatchers(functions1.getMenuUrl()).hasAuthority(functions1.getMenuName());
                }
        );
        //其它的都要验证
        registry.anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .defaultSuccessUrl("/index")
                .and()
                .logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login")
                .and().rememberMe()
                .tokenValiditySeconds(60)//按照你关闭浏览器后等待的时间
                .rememberMeParameter("rememberMe")
                .tokenRepository(persistentTokenRepository)
                .and()
                .csrf().disable();
        http.headers()
                .frameOptions()
                .sameOrigin()
                .httpStrictTransportSecurity()
                .disable();
    }

}
